<?php
/**
 * User: hjd-yao
 * Date: 2019/4/16
 * Time: 16:26
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopBrandModel;
use Illuminate\Http\Request;

class ShopBrandController extends BackendController
{
    public $brandModel;
    public function __construct()
    {
        $this->brandModel = new ShopBrandModel();
    }

    /**
     * @title 品牌管理
     * @return View
     */
    public function brands()
    {
        $list = $this->brandModel->paginate($this->getPerPageNum());
        return view('backend::shop.brand.list',[
            'page_title'=>'品牌列表',
            'list'=>$list
        ]);
    }

    /**
     * @title 添加品牌
     * @param Request $request
     * @return SelfResponse|View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $this->brandModel->name = $request->post('name');
            $this->brandModel->logo = $request->post('logo');
            $this->brandModel->url = $request->post('url');
            $this->brandModel->description = $request->post('description');
            $this->brandModel->sort = $request->post('sort',1000);
            $this->brandModel->is_hot = $request->post('is_hot',0);
            $this->brandModel->status = $request->post('status',1);
            if(!$this->brandModel->save()){
                return SelfResponse::error('品牌添加失败,请收好再试');
            }
            return SelfResponse::success('品牌添加成功','b_ShopBrand_brands');
        }else{
            return view('backend::shop.brand.add',[
                'page_title'=>'添加品牌'
            ]);
        }
    }

    /**
     * @title 编辑品牌
     * @param Request $request
     * @param $id
     * @return SelfResponse|View
     */
    public function update(Request $request, $id)
    {
        if($request->isMethod('post')){
            $info = ShopBrandModel::find($id);
            if(!$info){
                return SelfResponse::error('没有要修改的信息');
            }
            $info->name = $request->post('name');
            $info->logo = $request->post('logo');
            $info->url = $request->post('url');
            $info->description = $request->post('description');
            $info->sort = $request->post('sort',1000);
            $info->is_hot = $request->post('is_hot',0);
            $info->status = $request->post('status',1);
            if(!$info->save()){
                return SelfResponse::error('修改失败');
            }
            return SelfResponse::success('修改成功','b_ShopBrand_brands');
        }else{
            return view('backend::shop.brand.edit',[
                'page_title'=>'编辑品牌',
                'info'=>ShopBrandModel::find($id)
            ]);
        }
    }

    /**
     * @title 删除品牌
     * @param $id
     * @return SelfResponse
     */
    public function delete($id)
    {
        $info = ShopBrandModel::find($id);
        if(!$info){
            return SelfResponse::error('没有要修改的信息');
        }
        if(!$info->delete()){
            return SelfResponse::error('删除失败');
        }
        return SelfResponse::success('删除成功','b_ShopBrand_brands');
    }

    /**
     * @title 批量删除品牌
     * @param Request $request
     * @return SelfResponse
     */
    public function batchDelete(Request $request)
    {
        $ids = $request->input('ids');
        if (!$ids){
            return SelfResponse::error('请选择要删除的数据');
        }
        if(!ShopBrandModel::whereIn('id',$ids)->delete()){
            return SelfResponse::error('批量删除失败');
        }
        return SelfResponse::success('批量删除成功','b_ShopBrand_brands');
    }

    /**
     * @title 品牌启用/禁用
     * @param $id
     * @return SelfResponse
     */
    public function state($id)
    {
        $info = ShopBrandModel::find($id);
        if(!$info){
            return SelfResponse::error('没有要修改的信息');
        }
        $opt_title = $info->status?'禁用':'启用';
        $info->status = abs($info->status-1);
        if(!$info->save()){
            return SelfResponse::error($opt_title.'失败');
        }
        return SelfResponse::success($opt_title.'成功','b_ShopBrand_brands');
    }
}